/**
 * 
 */
package com.feib.soeasy.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.feib.soeasy.model.Payment;
import com.feib.soeasy.model.PaymentItem;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 * @author Leo Lee
 *
 */
public class SoezReportManager
{
    /**
     * 編譯 Jasper Report 原始檔
     * @param reportSource Jasper Report 原始檔(*.jrxml)
     * @return compiled report design object to use for filling
     * @throws JRException
     */
    public static JasperReport getCompiledJasperReport(String reportSource) 
        throws JRException
    {
        return JasperCompileManager.compileReport(reportSource);
    }
    
    /**
     * 
     * @param reportSource Jasper Report 原始檔(*.jrxml)
     * @param parameters 報表參數
     * @param dataSource 要列印的資料集
     * @return
     * @throws JRException
     */
    public static JasperPrint getJasperPrint(String reportSource, Map parameters
            , JRDataSource dataSource) throws JRException
    {
        return getJasperPrint(getCompiledJasperReport(reportSource),
                parameters, dataSource);
    }
    
    /**
     * 
     * @param jasperReport compiled report design object to use for filling
     * @param parameters 報表參數
     * @param dataSource 要列印的資料集
     * @return
     * @throws JRException
     */
    public static JasperPrint getJasperPrint(JasperReport jasperReport, Map parameters
            , JRDataSource dataSource) throws JRException
    {
        return JasperFillManager.fillReport(jasperReport,
                parameters, dataSource);
    }
    
    /**
     * 
     * @param reportSource Jasper Report 原始檔(*.jrxml)
     * @param parameters 報表參數
     * @param dataSource Jdbc connection
     * @return
     * @throws JRException
     */
    public static JasperPrint getJasperPrint(String reportSource, Map parameters
            , Connection connection) throws JRException
    {
        return getJasperPrint(getCompiledJasperReport(reportSource),
                parameters, connection);
    }
    
    /**
     * 
     * @param jasperReport compiled report design object to use for filling
     * @param parameters 報表參數
     * @param connection Jdbc connection
     * @return
     * @throws JRException
     */
    public static JasperPrint getJasperPrint(JasperReport jasperReport, Map parameters
            , Connection connection) throws JRException
    {
        return JasperFillManager.fillReport(jasperReport,
                parameters, connection);
    }
    
    /**
     * 直接產出PDF的資料流，非檔案！
     * @param reportSource 編譯 Jasper Report 原始檔
     * @param parameters 報表參數
     * @param data 要列印的資料集物件
     * @return
     * @throws JRException
     */
    public static byte[] exportReportToPdf(String reportSource, Map parameters, List data) throws JRException
    {
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(data);
        
        JasperPrint jasperPrint = getJasperPrint(reportSource, parameters, dataSource);
        
        return JasperExportManager.exportReportToPdf(jasperPrint);
    }
    
    /**
     * 產出PDF的檔案！
     * @param reportSource 編譯 Jasper Report 原始檔
     * @param parameters 報表參數
     * @param data 要列印的資料集物件
     * @param destFileName 檔案完整名稱(含路徑)
     * @return
     * @throws JRException
     */
    public static void exportReportToPdfFile(String reportSource, Map parameters, List data, String destFileName) throws JRException
    {
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(data);
        
        JasperPrint jasperPrint = getJasperPrint(reportSource, parameters, dataSource);
        
        JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName);        
    }
    
    /**
     * 直接產出PDF的資料流，非檔案！
     * @param reportSource 編譯 Jasper Report 原始檔
     * @param parameters 報表參數
     * @param data java.sql.ResultSet 型態的資料錄
     * @return
     * @throws JRException
     */
    public static byte[] exportReportToPdf(String reportSource, Map parameters, ResultSet data) throws JRException
    {
        JRResultSetDataSource dataSource = new JRResultSetDataSource(data);
        
        JasperPrint jasperPrint = getJasperPrint(reportSource, parameters, dataSource);
        
        return JasperExportManager.exportReportToPdf(jasperPrint);
    }
    
    /**
     * 產出PDF的檔案！
     * @param reportSource 編譯 Jasper Report 原始檔
     * @param parameters 報表參數
     * @param data java.sql.ResultSet 型態的資料錄
     * @param destFileName 檔案完整名稱(含路徑)
     * @throws JRException
     */
    public static void exportReportToPdfFile(String reportSource, Map parameters, ResultSet data, String destFileName) throws JRException
    {
        JRResultSetDataSource dataSource = new JRResultSetDataSource(data);
        
        JasperPrint jasperPrint = getJasperPrint(reportSource, parameters, dataSource);
        
        JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName);        
    }
    
    /**
     * 直接產出PDF的資料流，非檔案！
     * @param reportSource 編譯 Jasper Report 原始檔
     * @param parameters 報表參數
     * @param data java.sql.Connection jdbc connection，在報表裡面寫 sql，
     * 但是 sql 裡面不可以有參數的傳入，若 sql 裡要含有參數，請使用ResultSet
     * @return
     * @throws JRException
     */
    public static byte[] exportReportToPdf(String reportSource, Map parameters, Connection connection) throws JRException
    {
        
        JasperPrint jasperPrint = getJasperPrint(reportSource, parameters, connection);
        
        return JasperExportManager.exportReportToPdf(jasperPrint);
    }
    
    /**
     * 產出PDF的檔案！
     * @param reportSource 編譯 Jasper Report 原始檔
     * @param parameters 報表參數
     * @param data java.sql.Connection jdbc connection，在報表裡面寫 sql，
     * 但是 sql 裡面不可以有參數的傳入，若 sql 裡要含有參數，請使用ResultSet
     * @param destFileName 檔案完整名稱(含路徑)
     * @throws JRException
     */
    public static void exportReportToPdfFile(String reportSource, Map parameters, Connection connection, String destFileName) throws JRException
    {        
        JasperPrint jasperPrint = getJasperPrint(reportSource, parameters, connection);
        
        JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName);        
    }
    
    public static void main(String[] args) {
    
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = dateFormat.format(new Date());

    	String filename = "c:/temp/" + now + ".pdf";
    	
    	List<PaymentItem> items = new ArrayList<PaymentItem>();
    	List<Payment> payments = new ArrayList<Payment>();
    	Payment payment = new Payment();
    	
    	payment.setVirtualAccount("test account");
    	payment.setPayerName("test payername");
    	payment.setPaymentName("test payment name");
    	PaymentItem item = new PaymentItem();
    	item.setItemName("123asdf");
    	items.add(item);
    	item = new PaymentItem();
    	item.setItemName("abc1234asdf1");
    	items.add(item);
    	item = new PaymentItem();
    	item.setItemName("abc1234asdf2");
    	item.setLoanable(new Boolean(true));
    	items.add(item);
    	item = new PaymentItem();
    	item.setItemName("abc1234asdf3");
    	items.add(item);
    	item.setLoanable(new Boolean(true));
    	item = new PaymentItem();
    	item.setItemName("abc1234asdf4");
    	item.setLoanable(new Boolean(true));
    	items.add(item);
    	item = new PaymentItem();
    	item.setItemName("abc1234asaaaaaaadf1");
    	items.add(item);
    	
    	payment.setPaymentItems(items);
    	payments.add(payment);
    	
    	JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(payments);
		try {
			exportReportToPdfFile(
					"D:/MyProjects/feib/soeasy/src/main/webapp/reports/reporttest.jrxml"
					, null , payments , filename);
		} catch (JRException e) {
			// TODO 自動產生 catch 區塊
			e.printStackTrace();
		}
	}
}
